<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <!-- 匿名插槽 -->
    <el-form ref="RoleForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" style="width:80%" placeholder="请输入角色描述" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRoleList, getRoleDetail, editRoleList } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: { required: true, trigger: 'blur', message: '角色名不能为空' },
        description: { required: true, trigger: 'blur', message: '角色描述不能为空' }

      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑角色' : '新增角色'
    }
  },
  methods: {
    btnCancel() {
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.RoleForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    btnOK() {
      this.$refs.RoleForm.validate(async isOK => {
        if (isOK) {
          let info = ''
          if (this.formData.id) {
            info = '编辑'
            await editRoleList(this.formData)
          } else {
            info = '新增'
            await addRoleList(this.formData)
          }
          this.$message.success(`${info}角色成功`)
          this.$emit('addRole')
          this.$emit('update:showDialog', false)
        }
      })
    },
    async getRoleDetail(id) {
      this.formData = await getRoleDetail(id)
    }
  }
}
</script>

<style>

</style>
